package com.wd.buycar.view.fragment

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.wd.buycar.databinding.FragmentBuyOldCarBinding
import com.wd.buycar.view.activity.LoveCarActivity
import com.wd.buycar.view.adapter.BuyoldcarAdapterList
import com.wd.common.base.BaseFragment
import com.wd.common.viewmodel.MyViewModel


class BuyOldCarFragment : BaseFragment<FragmentBuyOldCarBinding>() {
    val viewmodel: MyViewModel by viewModels()
    private var oldcaradapter: BuyoldcarAdapterList? = null

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentBuyOldCarBinding = FragmentBuyOldCarBinding.inflate(layoutInflater)

    override fun initData() {

        viewmodel.buy_oldcarClooseList("1", 1, 8)

        binding.buyOldcarSearchButton.setOnClickListener {
            val s = binding.buyOldcarSearchBox.text.toString()
            if (s != null) {
                viewmodel.buy_oldcarClooseList(s, 1, 8)
            } else {
                ToastUtils.showLong("输入框为空")
            }
        }
        //二手车列表
        viewmodel.buy_oldcarClooseListLiveData.observe(this, Observer {
            oldcaradapter = context?.let { it1 -> BuyoldcarAdapterList(it1, it.result) }
            binding.buyOldcarRecy.layoutManager = LinearLayoutManager(context)
            binding.buyOldcarRecy.adapter = oldcaradapter
            if (it.result.size > 0) {
                binding.buyOldcarRecy.visibility = View.VISIBLE
                binding.buyOldcarGonetext.visibility = View.GONE
            } else {
                binding.buyOldcarRecy.visibility = View.GONE
                binding.buyOldcarGonetext.visibility = View.VISIBLE
            }
        })
        binding.buyOldcarLovecar.setOnClickListener {
            startActivity(Intent(context,LoveCarActivity::class.java))
        }
    }

}